#import "ReloginWaitingViewController.h"
#import "UserDao.h"
#import "PopUIManager.h"
#import "NetWorkManager.h"
#import "JsonUtil.h"
#import "LogInOutManager.h"
#import "MainViewCreater.h"
#import "RegLoginViewController.h"

@interface ReloginWaitingViewController ()

@end

@implementation ReloginWaitingViewController
@synthesize user_id,user_type,user_token;

- (void)viewDidLoad {
    [super viewDidLoad];
    //判断当前登录的用户是谁,根据当前用户名，去服务器上获取最新的数据，本地内存，数据库同步更新，最后进入主界面。
  //  [RequestServerAPI apiTeacherRelogin:self userId:user_id userType:user_type userToken:user_token];
   

}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSString *dataString = [JsonUtil convertDictionaryToJson:[[NSDictionary alloc]initWithObjectsAndKeys:@([[UserDao getInstance] getUserId]), @"uid", @(1), @"utype", [[UserDao getInstance]getAccessToken], @"token", nil]];
    

    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:self.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:PARENT_RELOGIN data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [popUIMgr removeWaitingUI];
        if (connectionError) { //连接失败
            
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:@"网络连接错误" cancelButtonTitle:@"确定"];
            [self reLogin];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                debugLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 1:{
                            ret_msg= @"Success Login";
                            debugLog(@"重新登录成功。");
                            //将服务器返回的字典重新传入教师园长登录接口，同步初始化内存、数据库。
                            NSMutableDictionary *dict_m = [[NSMutableDictionary alloc]initWithDictionary:dict];
                            [dict_m setObject:[dict objectForKey:@"nickname"] forKey:@"name"];
                            BOOL result = [[UserDao getInstance]saveUserData:dict_m];
                            NSArray *babyinfo = [dict objectForKey:@"babyinfo"];
                            NSLog(@">>$  %@",babyinfo);
                            
                            if(babyinfo!=nil && babyinfo.count>0){
                                for (NSDictionary* dicc in babyinfo) {
                                    [[BabyDao getInstance]saveBabyData:dicc];
                                }
                            }
                            if(result){
                                UIViewController *mainViewController = [[MainViewCreater getInstance]createMainView];
                                
                                UIWindow *window = [[UIApplication sharedApplication]keyWindow];
                                window.rootViewController = mainViewController;
                                
                            }else{
                                [popUIMgr popAlertView:nil title:@"提示" message:@"登录失败" cancelButtonTitle:@"确定"];
                            }
                            
                            break;
                        }case 2:{
                            ret_msg= @"用户名没找到";
                            break;
                        }case 3:{
                            ret_msg= @"用户名与密码不匹配";
                            break;
                        }case 4:{
                            ret_msg= @"账号已经停用";
                            break;
                        }case 10010:{
                            ret_msg= @"你的账号已经在其他地方登陆，请重新登陆账号。";
                            
                            //清空内存中的UserInfo数据，重置数据库中的hasLogin标志位是0，跳转到注册登陆界面，重新登陆。
                            
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success Login"]) {
                        [self reLogin];

                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                                           }
                }else{
                     [self reLogin];
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                   
                }
            }else{
                [self reLogin];
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
                
            }
        }
        
    }];
}
-(void)reLogin
{
    [[UserDao getInstance]logOut];
    RegLoginViewController *reg = [[RegLoginViewController alloc]init];
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:reg];
    UIWindow *window = [[UIApplication sharedApplication]keyWindow];
    window.rootViewController = nav;
}
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
